package logic.nodes.lod.blocks;

import gameStates.absGamesStates.AbsIngameState;
import logic.nodes.collision.CollidableNode;

import com.jme.math.Vector3f;

public class Block extends AbstractBlock {
		
	private AbstractBlock blockA, blockB;
	
	public Block(Vector3f center, float xExtend, float yExtend, float zExtend, AbsIngameState ingameState) {
		super(center, xExtend, yExtend, zExtend, ingameState);
	}
	
	public Block(Vector3f center, float size, AbsIngameState ingameState) {
		super(center, size, ingameState);
	}
	
	public void addBlock(AbstractBlock block) {
		assert(intersectsWith(block));
		
		if(blockA == null) blockA = block;
		else {
			assert(blockB == null);
			blockB = block;
		}
		block.setParentBlock(this);
	}
	
	public void searchLeafBlocks(CollidableNode node) {
		if(blockA instanceof Block) {
			checkBlock((Block)blockA, node);
			checkBlock((Block)blockB, node);
		} else {
			checkLeaf((LeafBlock)blockA, node);
			checkLeaf((LeafBlock)blockB, node);
		}
	}
	
	protected void checkLeaf(LeafBlock block, CollidableNode node) {
		if(block.intersectsWith(node)) block.addLeafNode(node);
	}
	
	protected void checkBlock(Block block, CollidableNode node) {
		if(block.intersectsWith(node)) block.searchLeafBlocks(node);
	}
	
	public AbstractBlock getBlockA() { return blockA; }
	
	public AbstractBlock getBlockB() { return blockB; }
}